var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cate: null,
    show_agreement: 0
  },
  backto: function (e) {
    wx.navigateBack({})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.showLoading({
      title: '加载中…',
    })
    if (!app.globalData.userInfo) {
      app.getUserInfo();
    }
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/cate?type=index&need_sub=1&token=' + this.data.token,
      success: res => {
        this.setData({
          cate: res.data
        })
        wx.hideLoading();
      }
    });
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getShopAgreementF", function (res) {
      var data = res.data;
      var WxParse = require('../../wxParse/wxParse.js');
      var article = data.content;
      WxParse.wxParse('article', 'html', article, that, 5);
      that.setData({
        shop_agreement: data
      })
    })
  },
  add: function(e){
    console.log(e);
    let that = this;
    var idx = e.currentTarget.dataset.idx;
    var cate = this.data.cate[idx];
    var add_url = "/pages/index/common/add?id=" + cate.id;
    if(cate.sub.length > 0){
      var sub = [];
      cate.sub.forEach(function(vo,key){
        sub.push(vo.title);
      })
      wx.showActionSheet({
        itemList: sub,
        success: function (res) {
          if (res.tapIndex !== undefined){
            var sub_cate = that.data.cate[idx].sub[res.tapIndex];
            var add_url = "/pages/index/common/add?id=" + sub_cate.id;
            wx.navigateTo({
              url: add_url,
            }) 
          }
        }
      })
    }else{
      wx.navigateTo({
        url: add_url,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  show_agreement: function () {
    this.setData({
      show_agreement: 1
    })
  },
  hiden_agreement: function () {
    this.setData({
      show_agreement: 0
    })
  }
})